//
//  HOTCloseDownViewController.m
//  Genpara
//
//  Created by AndyChang.zhang on 14-9-4.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTCloseDownViewController.h"
#import "HOTCloseDownCell.h"
#import "HOTCloseDownSubListViewController.h"

//#define _pageNo 1
#define _pageCnt 10

@interface HOTCloseDownViewController () <UITableViewDataSource, UITableViewDelegate> {
    
    //NSArray *dataList;
    BOOL isRegisted;
    int _pageNo;
    
    NSInteger curtYear;
    NSInteger curtMonth;
}
@property(nonatomic,strong)NSMutableArray *dataList;
@end

@implementation HOTCloseDownViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setNaviTitle:@"俺の現場がヤバい！！" fontSize:24 fontName:HOTTitleFont_ROUNDED_X];
    
    _dataList = [[NSMutableArray alloc] initWithCapacity:10];
    
    _tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_bankruptcy"]];
    
#if 0
    //self.dataList = @[@"2014年7月",@"2014年6月",@"2014年5月",@"2014年4月",@"2014年3月",@"2014年2月",@"2014年1月"];
    
    //[_tableView reloadData];
#endif
    //_tableView.showFooter = YES;
    _tableView.showHeader = YES;

    __unsafe_unretained HOTCloseDownViewController *tempObj = self;
    _tableView.headerView.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {

        
        [tempObj requestDataFromNet];
//        NSArray *newDateList = [self createDateWithPageNo];
//        [_dataList addObjectsFromArray:newDateList];
//        [_tableView.footerView endRefreshing];
//        [_tableView reloadData];
    };

    _dataList = [NSMutableArray array];
//    NSArray *newDateList = [self createDateWithPageNo];
//    [_dataList addObjectsFromArray:newDateList];
    
    [self requestDataFromNet];
}

-(void)requestDataFromNet {
    
    [self showLoading];
    
    NSDictionary *parameters = @{REQ_KEY_USER_TOKEN:self.userInfo.token,
                                 REQ_KEY_USER_ID:self.userInfo.userId
                                 };
    
    __unsafe_unretained HOTCloseDownViewController *tempObj = self;
    [self requestWithURL:HOT_URL_CLOSURE_INDEX Parameters:parameters success:^(NSInteger result, NSArray * dataArr) {
        
        [tempObj loadingSuccess];
        [_tableView.headerView endRefreshing];
        //[_tableView.footerView endRefreshing];
        if (dataArr.count == 0) {
            
            [self alertNoDataMessage:^{
                //[tempObj.navigationController popViewControllerAnimated:YES];
            }];

            return;
        }
        
        
        
        [tempObj.dataList removeAllObjects];
        
        
        for (int i = 0; i < dataArr.count; i++) {
            
            NSString *dateStr = [dataArr objectAtIndex:i];
            
            NSDate *date = [ZZZUtils formartStr:dateStr withStyle:@"yyyyMM"];
            
            NSString *formartDate = [ZZZUtils formartDate:date withStyle:@"yyyy年MM月"];
            //if (i*_pageCnt >= (pageNo-1)*_pageCnt) {
            
            NSDictionary *dic = @{@"formartDate":formartDate,
                                  @"date":dateStr};
            
            [tempObj.dataList addObject:dic];
            //}
            
        }

        //[tempObj.dataList addObjectsFromArray:dataArr];
        
        [_tableView reloadData];
        
    } failure:^(NSError *error, NSString *errMsg) {
        
        [tempObj loadingSuccess];
        [_tableView.headerView endRefreshing];
        
        if ([tempObj checkErrResult:error errMsg:errMsg]) {
            return ;
        }
        
        if (errMsg.length > 0) {
            
            ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                                  message:errMsg
                                                                 delegate:nil
                                                        cancelButtonTitle:nil
                                                         otherButtonTitle:ALERT_BTN_OK];
            
            [popupView showInWindow];
        }
    }];
}

/*
-(NSArray *)createDateWithPageNo {
    
    
    if (_pageNo == 0) {
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth fromDate:[NSDate date]];
        
        curtYear = components.year;
        curtMonth = components.month;
    }
    _pageNo ++;
    int pageNo = _pageNo;
    NSMutableArray *dateList = [[NSMutableArray alloc] initWithCapacity:_pageCnt];
    for (int i =(pageNo-1)*_pageCnt; i < pageNo*_pageCnt; i++) {

        //if (i*_pageCnt >= (pageNo-1)*_pageCnt) {
            
            NSDictionary *dic = @{@"formartDate":[NSString stringWithFormat:@"%ld年%ld月",(long)curtYear, (long)curtMonth],
                                  @"date":[NSString stringWithFormat:@"%ld%@",(long)curtYear, (curtMonth <10 ? [NSString stringWithFormat:@"0%ld", (long)curtMonth] : [NSString stringWithFormat:@"%ld", (long)curtMonth])]};
            
            [dateList addObject:dic];
        //}
        
        curtMonth --;
        if (curtMonth == 0) {
            
            curtMonth = 12;
            curtYear --;
        }
    }
    
    return dateList;
}
 */

#pragma mark - UItableView DataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellId = @"cellID";
    HOTCloseDownCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == nil) {
        
        UINib *nib = [UINib nibWithNibName:XIB(@"HOTCloseDownCell") bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:cellId];
        cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        cell.selectedBackgroundView = nil;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
    NSDictionary *dateDic = [self.dataList objectAtIndex:indexPath.row];
    NSString *titleStr = [dateDic objectForKey:@"formartDate"];
    cell.contentLabel.text = titleStr;
    
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataList.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 52;
}

#pragma mark - UITableView Delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *selDateDic = [self.dataList objectAtIndex:indexPath.row];
    NSString *selDate = [selDateDic objectForKey:@"date"];
    
    HOTCloseDownSubListViewController *cdslvc = [[HOTCloseDownSubListViewController alloc] initWithNibName:@"HOTCloseDownSubListViewController" bundle:nil selDate:selDate];
    
    [self.navigationController pushViewController:cdslvc animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
